<?php

namespace app\admin\controller\deerhome;

use app\common\controller\Backend;
use think\Db;
use Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
 * 零售百货订单管理
 *
 * @icon fa fa-circle-o
 */
class GoodsOrder extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\deerhome\GoodsOrder
     */
    protected $model = null;

    protected $searchFields = 'sn,user_phone';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\deerhome\GoodsOrder;
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    public function index()
    {
        if (!$this->request->isAjax())
        {
            return $this->view->fetch();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $total = $this->model
                ->where($where)
                ->where("order_type",2)
                ->count();
        $list = $this->model
                ->where($where)
                ->where("order_type",2)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
        $result = array("total" => $total, "rows" => $list);

        return json($result);
    }

    public function detail()
    {
        $sn=$this->request->get("sn");
        $row = $this->model->get(['sn'=>$sn]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $mapKey=configDeer("qq_map_key");
        $row['imgs']=explode(",",$row['imgs']);
        $row['imgs']=array_filter($row['imgs']);
        $row['worker_imgs']=explode(",",$row['worker_imgs']);
        $row['worker_imgs']=array_filter($row['worker_imgs']);
        $row['status_txt']=$this->model->getStatusTextAttr($row['status']);
        $row['pay_log']=Db::name("deerhome_order_pay_log")->where("order_sn",$row['sn'])->order("id desc")->select();
        $this->view->assign('row', $row);
        $this->view->assign('mapKey', $mapKey);
        return $this->view->fetch();
    }
    public function log($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (!$this->request->isAjax())
        {
            $logs = Db::name("deerhome_order_log")->where("order_id",$row['id'])->order("id desc")->select();
            $this->view->assign('logs', $logs);
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
    }
    public function fahuo($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (!$this->request->isAjax())
        {
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        if(!in_array($row['status'],[2,4])){
            $this->error('该订单状态不可发货');
        }
        if(!in_array($params['status'],[1,2])){
            $this->error('不支持的操作方式');
        }
        if($row['order_type']!=2){
            $this->error('不支持的订单类型');
        }
       
        if($params['status']==1){
            
        }
        $arr=[];
        $arr['status']=4;
        $arr['kd_gs']="无需快递";
        $arr['kd_sn']="无需快递";
        $arr['kd_time']=\date("Y-m-d H:i:s");
        if($params['status']==1){
            $arr['kd_gs']=$params['kd_gs'];
            $arr['kd_sn']=$params['kd_sn'];
        }
        if($arr['kd_gs']=="" ||  $arr['kd_sn']==""){
            $this->error('请填写快递信息');
        }

        $admin=$this->auth->getUserInfo();
        Db::startTrans();
        try{
            Db::name("deerhome_order")->where("id",$row['id'])->update($arr);
            Db::name("deerhome_order_log")->insert([
                'order_id'=>$row['id'],
                'add_time'=>date("Y-m-d H:i:s"),
                'ac_user'=>'管理员:'.$admin['username'],
                'note'=>"已发货，请留意快递信息"
            ]);
            //通知用户
            // \Tools\Msg::toUserPaidan($row['jz_user_id'],$row['sn']);
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success();
    }


}
